package com.hzw.saas.web.im.im.websocket;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author sonam
 * @sine 2021/10/21 12:39 下午
 */
@Slf4j
@Component
@ChannelHandler.Sharable
public class HeartBeatHandler extends ChannelInboundHandlerAdapter {
    private ConcurrentHashMap<Channel,Integer> channelHeartbeat = new ConcurrentHashMap();
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
            String eventMsg = null;
            switch (idleStateEvent.state()) {
                case ALL_IDLE:
                    eventMsg = "读写空闲"; // TODO
                    break;
                case READER_IDLE:
                    eventMsg = "读空闲"; // TODO
                    break;
                case WRITER_IDLE:
                    eventMsg = "写空闲"; // TODO
                    break;
                default:
                    break;
            }
            log.debug(ctx.channel().id().asLongText() + ":" + eventMsg);
        }
    }
}
